var common = require('../../utils/common.js');
const app = getApp()
Page({
  data: {
    classification: [],
    current: 0,
    items: [],
    onlyWx: false,
    itemloading: true,
    itemIsShow: false,
    page: 1,
    categoryId: '',
    userInfo: app.globalData.userInfo,
    tipshow: true,
    more: true,
    currentTab: 0,
    rToolsShow: false,
    nineloading: true,
    currentTab: 0,
    todayitems:[],
    nineitems:[],
    seachitems:[],
    itemloading: true,
    todayloading: true,
    nineloading: true,
    seachloading: true,
    from: '',
    jxpage: 1,
    todayPageNum: 1,
    skPageNum: 1,
    
  },
  onLoad: function (options) {
    console.log(options)
    this.setData({
      from: options.from
    })
    this.getclassificationdata()
    console.log(app.globalData.userInfo)
    this.setData({
      userInfo: app.globalData.userInfo
    })
  },
  getclassificationdata() {
    const that = this
    wx.getStorage({
      key: 'classification',
      success: function (res) {
        console.log(res.data)
        that.setData({
          classification: res.data,
        });
        if(that.data.from == 'free'){
          that.getnineitemdata()
          that.setData({
            currentTab: 2
          })
        }else{
          that.getitemdata(res.data[0].id)
        }
      },
      fail: function (res) {
        console.log(res.data)
        common.post({
          url: '/app/v2/goods/categorys', data: {}
        })
          .then((res) => {
            console.log(res.data)
            that.setData({
              classification: res.data,
            });
            wx.setStorage({
              key: "classification",
              data: res.data
            })
            if(that.data.from == 'free'){
              that.getnineitemdata()
              that.setData({
                currentTab: 2
              })
            }else{
              that.getitemdata(res.data[0].id)
            }
            
          }).catch((res) => {
            console.log(res)
          })
      }
    })

  },
  swichNav(e) {
    if (this.data.currentTab !== e.currentTarget.dataset.current) {
      this.setData({
        currentTab: e.currentTarget.dataset.current
      })
      if (e.currentTarget.dataset.current == 0) {
        this.getitemdata()
      }
      if (e.currentTarget.dataset.current == 1) {
        this.gettodayitemdata()
      }
      if (e.currentTarget.dataset.current == 2) {
        this.getnineitemdata()
      }
      if (e.currentTarget.dataset.current > 2){
        this.getseitemdata(e.currentTarget.dataset.id)
      }
    }else{

    }
  },
  getitemdata(){
    const that = this
    common.post({
      url: '/app/v2/goods/list', data: { pageNo: 1,isChoiceness: true,sessionId:app.globalData.sessionId }
    })
      .then((res) => {
        console.log(res.data)
        that.setData({
          items: res.data.data,
        });
        console.log(that.data.items)
      }).catch((res) => {
        console.log(res)
      })
  },
  getitemdatapage(){
    const that = this;
    var page = that.data.jxpage + 1;
    common.post({ url: '/app/v2/goods/list', data: { isChoiceness: true, pageNo: page,sessionId:app.globalData.sessionId } })
      .then((res) => {
        var data = that.data.items;
        console.log(res.data)
        if (that.data.jxpage * 10 < res.data.total) {
          that.setData({
            jxpage: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
          }
          setTimeout(() => {
            console.log(data)
            that.setData({
              items: data,
            })
          }, 300);
        } else {
          console.log('没有了')
          that.setData({
            itemloading: false
          })
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  gettodayitemdata() {
    const that = this
    common.post({
      url: '/app/goods/today', data: { pageNo: 1,sessionId:app.globalData.sessionId }
    })
      .then((res) => {
        console.log(res.data)
        that.setData({
          todayitems: res.data.data,
        });
        console.log(that.data.todayitems)
      }).catch((res) => {
        console.log(res)
      })
  },
  gettodayitemdatapage() {
    const that = this;
    var page = that.data.todayPageNum + 1;
    common.post({ url: '/app/goods/today', data: {pageNo: page,sessionId:app.globalData.sessionId } })
      .then((res) => {
        var data = that.data.items;
        console.log(res.data)
        if (that.data.todayPageNum * 10 < res.data.total) {
          that.setData({
            todayPageNum: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
          }
          setTimeout(() => {
            console.log(data)
            that.setData({
              todayitems: data,
            })
          }, 300);
        } else {
          console.log('没有了')
          that.setData({
            todayloading: false
          })
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  getnineitemdata() {
    const that = this
    isChoiceness = false
    common.post({
      url: '/app/v2/goods/freeShipping99', data: { pageNo: 1,sessionId:app.globalData.sessionId}
    })
      .then((res) => {
        that.setData({
          nineitems: res.data.data,
          loading: false
        });
        console.log(that.data.nineitems)
      }).catch((res) => {
        console.log(res)
      })
  },
  getnineitemdatapage() {
    const that = this;
    var page = that.data.skPageNum + 1;
    common.post({ url: '/app/v2/goods/freeShipping99', data: {pageNo: page,sessionId:app.globalData.sessionId } })
      .then((res) => {
        var data = that.data.nineitems;
        console.log(res.data)
        if (that.data.skPageNum * 10 < res.data.total) {
          that.setData({
            skPageNum: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
          }
          setTimeout(() => {
            console.log(data)
            that.setData({
              nineitems: data,
            })
          }, 300);
        } else {
          console.log('没有了')
          that.setData({
            nineloading: false
          })
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  getseitemdata(categoryId) {
    const that = this
    this.setData({
      seachitems: [],
      categoryId: categoryId
    });
    common.post({
      url: '/app/v2/goods/list', data: { categoryId: categoryId, onlyWx: this.data.onlyWx, pageNo: 1,sessionId:app.globalData.sessionId }
    })
      .then((res) => {
        console.log(res.data)
        that.setData({
          seachitems: res.data.data,
          loading: false,
          itemIsShow: true
        });
      }).catch((res) => {
        console.log(res)
      })
  },
  getseitemdatapage(categoryId) {
    const that = this;
    var page = that.data.page + 1;
    common.post({ url: '/app/v2/goods/list', data: { categoryId: categoryId, onlyWx: this.data.onlyWx, pageNo: page,sessionId:app.globalData.sessionId } })
      .then((res) => {
        var data = that.data.seachitems;
        console.log(res.data)
        if (that.data.page * 10 < res.data.total) {
          that.setData({
            page: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
            setTimeout(function () {
              that.setData({
                seachitems: data
              });
              console.log(data)
            }, 300)
          }
        } else {
          console.log('没有了')
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  getMore() {
    console.log('bottom')
    console.log(this.data.currentTab)
    if(this.data.currentTab == 0){
      this.getitemdatapage()
    }
    if(this.data.currentTab == 1){
      this.gettodayitemdatapage()
    }
    if(this.data.currentTab == 2){
      this.getnineitemdatapage()
    }
    if(this.data.currentTab > 2){
      this.getseitemdatapage()
    }
  },
  buybtn(e) {
    if (e.currentTarget.dataset.item.is99) {
      wx.navigateTo({
        url: '/pages/activeitemdetail/activeitemdetail?sku=' + e.currentTarget.dataset.item.skuId
      })
    } else {
      wx.navigateTo({
        url: '/pages/itemdetailNew/itemdetailNew?sku=' + e.currentTarget.dataset.item.skuId
      })
    }
  },
  toMyStone() {
    wx.switchTab({
      url: '/pages/myStore/myStore',
      success: function (res) {
        console.log('toMyStore')
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  closeTip(){
    this.setData({
      tipshow: false
    })
  },
  totop(){
    this.setData({
      scrollTop: 0
    })
  },
  scolly: function (e) {
    // console.log(e.detail.scrollTop)
    if(e.detail.scrollTop > 600){
      this.setData({
        rToolsShow: true
      })
    }else{
      this.setData({
        rToolsShow: false
      })
    }
  },
  seachlink: function () {
    wx.navigateTo({
      url: '/pages/seach/seach'
    })
  },
  addShop(e){
    console.log(e.currentTarget.dataset.item.skuId)
    var skuId = e.currentTarget.dataset.item.skuId
    var i = e.currentTarget.dataset.index
    const that = this
    common.post({
      url: '/app/v2/shop/addProduct', data: { sessionId: app.globalData.sessionId, skuId: skuId}
    })
      .then((res) => {
        console.log(res.data)
        if (res.data.code == "0") {
          wx.showToast({
            title: '已加入店铺',
            icon: 'success',
            duration: 2000
          })
          if(this.data.currentTab == 0){
            that.data.items[i].shopHas = true
            that.setData({
              items: that.data.items
            })
          }
          if(this.data.currentTab == 1){
            that.data.todayitems[i].shopHas = true
            that.setData({
              todayitems: that.data.todayitems
            })
          }
          if(this.data.currentTab == 2){
            that.data.nineitems[i].shopHas = true
            that.setData({
              nineitems: that.data.nineitems
            })
          }
          if(this.data.currentTab > 2){
            that.data.seachitems[i].shopHas = true
            that.setData({
              seachitems: that.data.seachitems
            })
          }
          
        }
      }).catch((res) => {
        console.log(res)
      })
  },
})